package com.itheima.domain;


//测试类

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;

public class DataSourceDemo {


    //    1.druid连接
    @Test
    public void testIocDruid() throws Exception {
        //1、读取spring配置文件 创建IOC容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        //2、从IOC容器中获取数据源对象   DataSource.class 可以不使用类型强转
        DataSource ds = ac.getBean("dataSource", DataSource.class);
        //创建连接
        Connection connection = ds.getConnection();
//        打印连接池查看信息
        System.out.println(connection);
    }

    //    2. 连接c3p0
    @Test
    public void testIocC3p0() throws Exception {
        //1、创建IOC容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        //2、从IOC容器中获取数据源对象
        DataSource ds = ac.getBean("dataSource1", DataSource.class);
        Connection connection = ds.getConnection();
        System.out.println(connection);

    }
}
